package cn.itrip.controller;

import cn.itrip.commons.DtoUtils;
import cn.itrip.dto.Dto;
import cn.itrip.entity.ItripHotelOrder;
import cn.itrip.service.ItripHotelOrderService;
import cn.itrip.dto.HotelOrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Super God Jie
 * @version 1.0  2022-04-15 16:19
 */
@RestController
@RequestMapping("/api/hotelorder")
public class HotelOrderController {

	@Resource
	private ItripHotelOrderService itripHotelOrderService;

	@PostMapping("/getpreorderinfo")
	public Dto getpreorderinfo(@RequestBody Map<String,String> map, HttpServletRequest request){
		String token = request.getHeader("token");
		try {
			return DtoUtils.returnSuccess(itripHotelOrderService.getpreorderinfo(map,token));
		} catch (Exception e) {
			return DtoUtils.returnFail("30001",e.getMessage());
		}
	}

	@PostMapping("/addhotelorder")
	public Dto addhotelorder(@RequestBody HotelOrderDTO hotelOrderDto, HttpServletRequest request){
		String token = request.getHeader("token");
		try {
			ItripHotelOrder	orderService = itripHotelOrderService.itriptxCreateOrderService(hotelOrderDto,token);
			System.out.println("orderService = " + orderService);
			Map<String, Object> result = new HashMap<>();
			result.put("id",orderService.getId());
			result.put("orderNo",orderService.getOrderNo());
			return DtoUtils.ok("生成订单成功",result);
		} catch (Exception e) {
			return DtoUtils.returnFail("30002",e.getMessage());
		}
	}

	@GetMapping("/getpersonalorderroominfo/{orderId}")
	public Dto getpersonalorderroominfo(@PathVariable Long orderId){
		ItripHotelOrder itripHotelOrder = itripHotelOrderService.queryById(orderId);
		return DtoUtils.returnSuccess(this.hotelOrderConvertOrderDTO(itripHotelOrder));
	}

	public HotelOrderDTO hotelOrderConvertOrderDTO(ItripHotelOrder itripHotelOrder){
		HotelOrderDTO hotelOrderDTO = new HotelOrderDTO();
		BeanUtils.copyProperties(itripHotelOrder,hotelOrderDTO);
		return hotelOrderDTO;
	}
}
